﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Net;
using System.Net.Mail;
using Pub.Class;
using System.Collections;
using System.Linq;
using System.Text;
using GTS.MiniBox.Helper;
using GTS.MiniBox.Entity;

namespace GTS.MiniBox.Helper {
    /// <summary>
    /// 通道效果
    /// </summary>
    public class Base {
        /// <summary>
        /// 通道名称
        /// </summary>
        /// <param name="type"></param>
        /// <param name="channelID"></param>
        /// <returns></returns>
        public static string ChannelName(int type, int channelID) {
            switch (type) {
                case 1:
                    return SMSChannelInfoHelper.SelectListByAll("MiniBox")
                        .Where(p => p.ChannelID == channelID).FirstOrDefault().IfNull(new SMSChannelInfo()).ChannelName + "[SMS]";
                case 2:
                    return IVRChannelInfoHelper.SelectListByAll("MiniBox")
                    .Where(p => p.ChannelID == channelID).FirstOrDefault().IfNull(new IVRChannelInfo()).ChannelName + "[IVR]";
            }
            return "";
        }
        /// <summary>
        /// 通道名称(结账类型)
        /// </summary>
        /// <param name="type"></param>
        /// <param name="channelID"></param>
        /// <returns></returns>
        public static string ChannelNameTwo(int type, int channelID) {
            switch (type) {
                case 1:
                    SMSChannelInfo sms = SMSChannelInfoHelper.SelectListByAll("MiniBox")
                        .Where(p => p.ChannelID == channelID).FirstOrDefault().IfNull(new SMSChannelInfo());
                    return sms.ChannelName + "[SMS]" + (sms.Remark.IsNullEmpty() ? "" : " (" + sms.Remark + ") ");
                case 2:
                    IVRChannelInfo ivr = IVRChannelInfoHelper.SelectListByAll("MiniBox")
                    .Where(p => p.ChannelID == channelID).FirstOrDefault().IfNull(new IVRChannelInfo());
                    return ivr.ChannelName + "[IVR]" + (ivr.Remark.IsNullEmpty() ? "" : " (" + ivr.Remark + ") ");
            }
            return "";
        }
        /// <summary>
        /// 通道状态
        /// </summary>
        /// <param name="type"></param>
        /// <param name="channelID"></param>
        /// <returns></returns>
        public static int ChannelStatus(int type, int channelID) {
            switch (type) {
                case 1:
                    SMSChannelInfo sms = SMSChannelInfoHelper.SelectListByAll("MiniBox").Where(p => p.ChannelID == channelID).FirstOrDefault();
                    return sms.IsNull() ? 0 : sms.Status.Value;
                case 2:
                    IVRChannelInfo ivr = IVRChannelInfoHelper.SelectListByAll("MiniBox").Where(p => p.ChannelID == channelID).FirstOrDefault();
                    return ivr.IsNull() ? 0 : ivr.Status.Value;
            }
            return 0;
        }
        /// <summary>
        /// 返回通道使用的区域
        /// </summary>
        /// <param name="type"></param>
        /// <param name="channelID"></param>
        /// <param name="cstcode"></param>
        /// <returns></returns>
        public static string ChannelArea(int type, int channelID, int cstcode, string begin, string end) {
            IList<CityInfo> areaList = CityInfoHelper.SelectListByAll("MiniBox");
            string areaName = "";
            switch (type) {
                case 1:
                    areaName = SMSChannelInfoHelper.SelectListByAll("MiniBox")
                        .Where(p => p.ChannelID == channelID).FirstOrDefault().IfNull(new SMSChannelInfo()).UsePrivence; break;
                case 2:
                    areaName = IVRChannelInfoHelper.SelectListByAll("MiniBox")
                        .Where(p => p.ChannelID == channelID).FirstOrDefault().IfNull(new IVRChannelInfo()).UsePrivence; break;
            }
            StringBuilder html = new StringBuilder();
            if (!areaName.IsNullEmpty()) {
                areaName.ToStrings(';').Do(p => {
                    var info = areaList.Where(a => a.CityName == p).FirstOrDefault();
                    if (!info.IsNull() && !info.CityCode.IsNull()) {
                        html.AppendFormat("<a id='ViewChannelCity{0}' href='/admin/operations/minibox/channel-view.html?area={0}&cstcode={2}&begin={3}&end={4}' class='tab_open'>{1}</a>; ", info.CityCode, info.CityName, cstcode, begin, end);
                    }
                });
            }
            return html.ToString();
        }
        /// <summary>
        /// 不同颜色显示金额比
        /// </summary>
        /// <param name="rate"></param>
        /// <param name="sendCount"></param>
        /// <param name="spCount"></param>
        /// <returns></returns>
        public static string Rate(decimal rate, int sendCount, int spCount) {
            bool isTrue = (sendCount > 0 && rate < 1) || (sendCount < 100);
            return "<span class='{0}'>{1}%</span>".FormatWith(isTrue ? "red" : "", Math.Round(rate, 2));
        }
        /// <summary>
        /// 不同颜色显示金额比
        /// </summary>
        /// <param name="rate"></param>
        /// <param name="sendCount"></param>
        /// <param name="spCount"></param>
        /// <returns></returns>
        public static string RateColor(decimal rate, int sendCount, int spCount) {
            bool isTrue = (sendCount > 0 && rate < 1) || (sendCount < 100);
            return isTrue ? "red" : "";
        }
        /// <summary>
        /// 取区域
        /// </summary>
        /// <param name="cstcode"></param>
        /// <param name="cityID"></param>
        /// <returns></returns>
        public static string GetPrivenceCity(string cityID) {
            var info = CityInfoHelper.SelectListByAll("MiniBox").Where(p => p.CityCode == cityID).FirstOrDefault();
            return info.IsNull() || info.CityCode.IsNull() ? "" : (info.Province + " " + info.CityName);
        }
        /// <summary>
        /// 取区域
        /// </summary>
        /// <param name="cstcode"></param>
        /// <param name="cityID"></param>
        /// <returns></returns>
        public static string GetCity(string cityID) {
            var info = CityInfoHelper.SelectListByAll("MiniBox").Where(p => p.CityCode == cityID).FirstOrDefault();
            return info.IsNull() || info.CityCode.IsNull() ? "" : info.CityName;
        }
        /// <summary>
        /// 取运营商
        /// </summary>
        /// <param name="cstcode"></param>
        /// <returns></returns>
        public static string GetCstName(int cstcode) {
            var info = CstInfoHelper.SelectListByAll("MiniBox").Where(p => p.CstCode == cstcode).FirstOrDefault();
            return info.IsNull() || info.CstCode.IsNull() ? cstcode.ToString() : info.CstName;
        }
        /// <summary>
        /// SP
        /// </summary>
        /// <param name="cstcode"></param>
        /// <returns></returns>
        public static string GetSpName(string spcode) {
            var info = SPInfoHelper.SelectListByAll("ChannelApi").Where(p => p.SPCode == spcode).FirstOrDefault();
            return info.IsNull() || info.SPCode.IsNull() ? spcode : info.SPDesc;
        }
        /// <summary>
        /// 取运批次名称
        /// </summary>
        /// <param name="cstcode"></param>
        /// <returns></returns>
        public static string GetOriginName(int originID) {
            var info = OriginHelper.SelectListByAll("MiniBox").Where(p => p.OriginID == originID).FirstOrDefault();
            return info.IsNull() || info.OriginID.IsNull() ? "" : info.OriginName;
        }
        /// <summary>
        /// 取运批次名称、平台、计费类型
        /// </summary>
        /// <param name="cstcode"></param>
        /// <returns></returns>
        public static string GetOrigin(int originID) {
            var info = OriginHelper.SelectListByAll("MiniBox").Where(p => p.OriginID == originID).FirstOrDefault();
            return info.IsNull() || info.OriginID.IsNull() ? originID.ToString() : info.OriginID + " " + info.OriginName + "(" + info.PlatForm + " " + info.ChargeType + " " + info.BoxVer + ")";
        }
        /// <summary>
        /// 取省份
        /// </summary>
        /// <param name="cstcode"></param>
        /// <param name="cityID"></param>
        /// <returns></returns>
        public static string GetProvince(int provinceCode) {
            var info = CityInfoHelper.SelectListByAll("MiniBox").Where(p => p.ProvinceCode == provinceCode).FirstOrDefault();
            return info.IsNull() || info.CityCode.IsNull() ? "" : info.Province;
        }
        /// <summary>
        /// 输出下行数据
        /// </summary>
        /// <param name="data"></param>
        public static void Write(string data) {
            HttpContext.Current.Response.ContentType = "text/plain";
            HttpContext.Current.Response.AddHeader("SPECIAL", "GTSBOX");
            Msg.WriteEnd(data);
        }
        /// <summary>
        /// 4box输出下行
        /// </summary>
        /// <param name="data"></param>
        public static void SendDown(string data) {
            StringBuilder xml = new StringBuilder();
            xml.Append("<down>");
            xml.Append(data);
            xml.Append("</down>");
            Write(xml.ToString());
        }
        /// <summary>
        /// 4box输出二次确认下行
        /// </summary>
        /// <param name="data"></param>
        public static void SendDoubleDown(string data) {
            StringBuilder xml = new StringBuilder();
            xml.Append("<double_down>");
            xml.Append(data);
            xml.Append("</double_down>");
            Write(xml.ToString());
        }
        /// <summary>
        /// 3box输出二次确认下行
        /// </summary>
        /// <param name="data"></param>
        public static void SendThreeDoubleDown(string data) {
            StringBuilder xml = new StringBuilder();
            xml.Append("<double_sms_list>");
            xml.Append(data);
            xml.Append("</double_sms_list>");
            Write(xml.ToString());
        }
        /// <summary>
        /// 随机数
        /// </summary>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static int RndInt(int min, int max) {
            if (min == max && min == 0) return 0;
            if (min == max || max < min) return min;
            return Rand.RndInt(min, max);
        }
        /// <summary>
        /// 4box通用屏蔽
        /// </summary>
        /// <param name="cstcode"></param>
        /// <returns></returns>
        public static string GlobalBlock(int cstcode) {
            StringBuilder xml = new StringBuilder();
            switch (cstcode) {
                case 1:
                    xml.Append("<chinese data=\"处理\" />")
                        .Append("<chinese data=\"点播\" />")
                        .Append("<chinese data=\"确认点播，\" />")
                        .Append("<chinese data=\"确认信息\" />")
                        .Append("<chinese data=\"宣传\" />")
                        .Append("<chinese data=\"已点播\" />")
                        .Append("<chinese data=\"中国移动代收\" />")
                        .Append("<chinese data=\"中国移动提示\" />")
                        .Append("<chinese data=\"公司\" />")
                        .Append("<chinese data=\"业务\" />")
                        .Append("<chinese data=\"服务代码\" />")
                        .Append("<chinese data=\"客服电话\" />")
                        .Append("<chinese data=\"授权\" />")
                        .Append("<chinese data=\"本次请求\" />")
                        .Append("<chinese data=\"扣费提醒\" />");
                    break;
                case 2:
                    xml.Append("<chinese data=\"中国联通代收\" />")
                        .Append("<chinese data=\"中国联通提示\" />")
                        .Append("<chinese data=\"公司\" />")
                        .Append("<chinese data=\"业务\" />")
                        .Append("<chinese data=\"服务代码\" />")
                        .Append("<chinese data=\"客服电话\" />")
                        .Append("<chinese data=\"授权\" />")
                        .Append("<chinese data=\"本次请求\" />")
                        .Append("<chinese data=\"扣费提醒\" />");
                    break;
                default: break;
            }
            return xml.ToString();
        }
        /// <summary>
        /// 3box通用屏蔽
        /// </summary>
        /// <param name="cstcode"></param>
        /// <returns></returns>
        public static string ThreeGlobalBlock(int cstcode) {
            StringBuilder xml = new StringBuilder();
            switch (cstcode) {
                case 1:
                    xml.Append("<keyword>处理</keyword>")
                        .Append("<keyword>点播</keyword>")
                        .Append("<keyword>确认点播，</keyword>")
                        .Append("<keyword>确认信息</keyword>")
                        .Append("<keyword>宣传</keyword>")
                        .Append("<keyword>已点播</keyword>")
                        .Append("<keyword>中国移动代收</keyword>")
                        .Append("<keyword>中国移动提示</keyword>")
                        .Append("<keyword>公司</keyword>")
                        .Append("<keyword>业务</keyword>")
                        .Append("<keyword>服务代码</keyword>")
                        .Append("<keyword>客服电话</keyword>")
                        .Append("<keyword>授权</keyword>")
                        .Append("<keyword>本次请求</keyword>")
                        .Append("<keyword>扣费提醒</keyword>");
                    break;
                case 2:
                    xml.Append("<keyword>中国联通代收</keyword>")
                        .Append("<keyword>中国联通提示</keyword>")
                        .Append("<keyword>公司</keyword>")
                        .Append("<keyword>业务</keyword>")
                        .Append("<keyword>服务代码</keyword>")
                        .Append("<keyword>客服电话</keyword>")
                        .Append("<keyword>授权</keyword>")
                        .Append("<keyword>本次请求</keyword>")
                        .Append("<keyword>扣费提醒</keyword>");
                    break;
                default: break;
            }
            return xml.ToString();
        }
        /// <summary>
        /// 计费类型提示
        /// </summary>
        /// <param name="price"></param>
        /// <returns></returns>
        public static string GetChargebackTip(int price) {
            IList<ChargebackType> list = ChargebackTypeHelper.SelectListByAll("ConnString").Where(p => p.Price == price).ToList();
            return list.Count > 0 ? list[0].Tip.Replace("\"", "\\\"") : "";
        }
        /// <summary>
        /// 返回通道状态
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static string GetStatus(int i) {
            string status = "";
            switch (i) {
                case 0: status = "<span class=\"green\">可用</span>"; break;
                case 1: status = "<span class=\"red\">禁用</span>"; break;
                case 2: status = "<span class=\"red\">当天禁用</span>"; break;
            }
            return status;
        }
        /// <summary>
        /// 返回厂商名称
        /// </summary>
        /// <param name="factoryID"></param>
        /// <returns></returns>
        public static string GetFactory(int factoryID) {
            IList<FactoryInfo> list = FactoryInfoHelper.SelectListByAll("MiniBox").Where(p => p.FactoryID == factoryID).ToList();
            return list.Count > 0 ? list[0].Manufacturers : "";
        }
        /// <summary>
        /// 批次下次请求时间间隔
        /// </summary>
        /// <param name="origin"></param>
        /// <returns></returns>
        public static string GetTime(int origin) {
            if (origin == 1049 || origin == 1053) return "6";
            return "2";
        }
        /// <summary>
        /// IVR批次是否支持计费
        /// </summary>
        /// <param name="originid"></param>
        /// <returns></returns>
        public static bool IsIVRCharge(int originid) {
            IList<IVRChargeRule> list = IVRChargeRuleHelper.SelectListByAll("ConnString").Where(p => p.OriginID == originid).ToList();
            return list.Count > 0 ? false : true;
        }
    }
}
